/**   
* @Title: IPUtils.java
* @Package org.xaoyaoyao.cloud.common.utils
* @Description: TODO
* @author xaoyaoyao
* @date Feb 11, 2019 2:37:03 PM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.common.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: IPUtils
 * @Description: TODO
 * @author xaoyaoyao
 * @date Feb 11, 2019 2:37:03 PM
 * 
 */
public class IPUtils {

	public static String getRemoteIP(HttpServletRequest request) {
		if (null != request) {
			String ip = request.getHeader("x-forwarded-for");
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
				if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
					// 根据网卡获取本机配置的IP地址
					try {
						ip = InetAddress.getLocalHost().getHostAddress();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					}
				}
			}
			return ip;
		}
		return null;
	}

}
